package com.etc.controller;

import com.etc.entity.Result;
import com.etc.entity.Role;
import com.etc.entity.User;
import com.etc.service.RegisterService;
import com.etc.service.ShiorService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RegisterController {

    @Autowired
    private RegisterService registerService;
    @Autowired
    private ShiorService shiorService;
    @RequestMapping("/register")
    @ResponseBody
    public Result reg(@RequestBody User user) {
        String salt="123";
        System.out.println(user);
        Result result = new Result();
        if (" ".equals(user.getNameError()) && " ".equals(user.getPhoneError()) && " ".equals(user.getEmailError()) && " ".equals(user.getPasswordError())) {
            System.out.println("true");
            Role role = new Role();role.setRoleid(3);user.setRoleid(role);
            user.setPassword(new Md5Hash(user.getPassword(), salt).toHex());
            result.setError("1");
            shiorService.register(user);
//            registerService.saveRegister(user);
        } else {
            result.setError("2");
        }
        return result;
    }

    @RequestMapping("/registerPhone")
    @ResponseBody
    public Result regPhone(@RequestBody User user) {
        System.out.println(user.getPhone());
        boolean regPhone = registerService.regPhone(user.getPhone());
        Result result = new Result();
        if (!regPhone) {
            result.setResult("*手机号码已存在");
            return result;
        } else {
            result.setResult(" ");
            return result;
        }
    }

    @RequestMapping("/registerEmail")
    @ResponseBody
    public Result regEmail(@RequestBody User user) {
        System.out.println(user.getEmail());
        boolean regEmail = registerService.regEmail(user.getEmail());
        System.out.println(regEmail);
        Result result = new Result();
        if (!regEmail) {
            result.setResult("*邮箱地址已存在");
            return result;
        } else {
            result.setResult(" ");
            return result;
        }
    }

    @RequestMapping("/registerName")
    @ResponseBody
    public Result regName(@RequestBody User user) {
        String username = user.getUsername();
        System.out.println("C:" + username);
        boolean regName = registerService.regName(username);
        System.out.println("C:" + regName);
        Result result = new Result();
        if (!regName) {
            result.setResult("*用户名已存在");
            return result;
        } else {
            result.setResult(" ");
            return result;
        }
    }
}
